#include "xtypes.h"
#include "xstring.h"
#include "util_textparser.h"

bool_t xIsMbcsTextFile( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return FALSE;

	return FALSE;
}

bool_t xIsUtf8TextFile( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return FALSE;

	return FALSE;
}

bool_t xIsUnicodeTextFile( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return FALSE;

	return FALSE;
}

bool_t xIsUnicodeBigEndianTextFile( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return FALSE;

	return FALSE;
}



int_t xMbcsTextFile_SkipSpace( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return -1;

	while(_Iter<_Size)
	{
		char_t ch = *((char_t*)_DataPtr + _Iter);
		if( ch!=' ')
		{
			return _Iter;
		}

		_Iter++;
	}

	return -1;
}

int_t xMbcsTextFile_SkipTab( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return -1;

	while(_Iter<_Size)
	{
		char_t ch = *((char_t*)_DataPtr + _Iter);
		if( ch!='\t')
		{
			return _Iter;
		}

		_Iter++;
	}

	return -1;
}

int_t xMbcsTextFile_SkipSpaceTab( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return -1;

	while(_Iter<_Size)
	{
		char_t ch = *((char_t*)_DataPtr + _Iter);
		if( ch!='\t' && ch!=' ')
		{
			return _Iter;
		}

		_Iter++;
	}

	return -1;
}


int_t xMbcsTextFile_SkipInvisibleChar( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return -1;

	while(_Iter<_Size)
	{
		char_t ch = *((char_t*)_DataPtr + _Iter);
		if( ch > ' ' )
		{
			return _Iter;
		}

		_Iter++;
	}

	return -1;
}

int_t xMbcsTextFile_LocateCurLineHead( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return -1;


	return -1;
}


int_t xMbcsTextFile_LocateNextLine( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return -1;

	while(_Iter<_Size)
	{
		if( xMbcsTextFile_IsLineTerminator( _Iter, _DataPtr, _Size ) )
		{
			_Iter = xMbcsTextFile_SkipLineTerminator( _Iter, _DataPtr, _Size );
			return _Iter;
		}
	}

	return -1;
}

int_t xMbcsTextFile_SkipLineTerminator( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return -1;

	if( !xMbcsTextFile_IsLineTerminator( _Iter, _DataPtr, _Size ) )
		return -1;

	char_t ch = *((char_t*)_DataPtr + _Iter);
	if( ch=='\r' )
	{
		if( _Iter+1 >= _Size )
			return -1;
		char_t ch2 = *((char_t*)_DataPtr + _Iter + 1);
		
		_Iter++;

		if( ch2=='\n' )
			_Iter++;
	}
	else if( ch=='\n' )
	{
		_Iter++;
	}

	return _Iter>=_Size ? -1:_Iter;
}

bool_t xMbcsTextFile_IsLineTerminator( int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	if(_Iter < 0)
		return -1;

	char_t ch = *((char_t*)_DataPtr + _Iter);
	if( ch=='\r' || ch=='\n')
	{
		return TRUE;
	}

	return FALSE;
}


int_t xMbcsTextFile_GetCurLine( CxString& _Str, int_t _Iter, wildptr_t _DataPtr, int_t _Size )
{
	_Str.clear();

	if(_Iter < 0)
		return -1;

	while(_Iter<_Size)
	{
		if( !xMbcsTextFile_IsLineTerminator( _Iter, _DataPtr, _Size ) )
		{
			char_t ch = *((char_t*)_DataPtr + _Iter);
			_Str += ch;
		}
		else
		{
			_Iter = xMbcsTextFile_SkipLineTerminator( _Iter, _DataPtr, _Size );
			return _Iter;
		}

		_Iter++;
	}


	return -1;
}

int_t xMbcsTextFile_GetOneWord( CxString& _Str, lpcstr_t _szStopSymbol, int_t _Iter, wildptr_t _DataPtr, int_t _Size)
{
	_Str.clear();

	if(_Iter < 0)
		return -1;

	int_t iter = xMbcsTextFile_SkipInvisibleChar(_Iter, _DataPtr, _Size);
	if( iter==-1 )
	{
		return -1;
	}
	
	CxString strStopSymbol;
	strStopSymbol = _szStopSymbol;	

	while(_Iter<_Size)
	{
		char_t ch = *((char_t*)_DataPtr + _Iter);
		if( (ch <= ' ') || strStopSymbol.find(ch) != -1 )
		{
			_Iter++;
			return _Iter>=_Size ? -1:_Iter;
		}
		else
		{
			_Str += ch;
		}


		_Iter++;
	}

	_Str.clear();
	return -1;
}
